package com.yundian.user.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.yundian.dao.BaseDAO;
import com.yundian.user.dao.IUserDAO;
import com.yundian.user.vo.User;

public class UserDAO extends BaseDAO implements IUserDAO {

	public void saveUser(User user) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(user);
		tx.commit();
		session.close();
	}

	public User validateUser(String username, String password) {
		Session session = getSession();
		String hql = "from User u where u.username=? and u.password=?";
		Query query = session.createQuery(hql);
		query.setParameter(0,username);
		query.setParameter(1,password);
		List users = query.list();
		if(users.size() != 0){
			
			User user = (User)users.get(0);
			session.close();
			return user;
		}
		session.close();
		return null;
	}

}
